<?php
/**
 * @author bbernath
 *         Date: 03.11.13
 *         Time: 17:16
 * @version 1.0.6
 */

require_once('include/recaptchalib.php');
$publickey = "6LeEvOkSAAAAAFQolJFZL9tp-wScj0of6S4_eop4";

echo "<h3>&raquo; Kommentare</h3>";
if (isset($_GET['newsid'])) {
    $ID = $_GET['newsid'];
    $sql = "select n.id, n.topic, n.content as news_content, u.name as user_name, n.written, nc.name as news_category_name,
            u.email as user_email from news n, user u, news_category nc
            where n.user = u.id
            and n.news_category = nc.id
            and n.id = ?";
    if (!$stmt = $db->prepare($sql)) {
        echo $db->error;
    }
    $stmt->bind_param('i', $ID);
    if (!$stmt->execute()) {
        echo $stmt->error;
    }

    $stmt->bind_result($id, $topic, $news_content, $user_name, $written, $news_category_name, $user_email);
    if (!$stmt->fetch()) {
        echo 'Es wurde keine News mit der angegebenen ID gefunden.';
    }

    echo "<table border='1' id='news'>";
    echo "<tr><td><h3>" . $topic . "</h3></td></tr>";
    echo "<tr><td><br>" . $news_content . "<br><br></td></tr>";
    $date = new DateTime($written);
    echo "<tr><td>Von <a href='mailto:" . $user_email . "'>" . $user_name . "</a> am " . strftime('%A, %d.%B %Y um %H:%M', $date->getTimestamp()) . " Uhr</td></tr>";
    echo "</table>";
    echo "<table><tr><td><br></td></tr></table>";
    $stmt->close();

    echo "<hr><br>";

    $sql = "select id, content, email, name, written
            from news_comments
            where news = ?
            order by id asc";
    if (!$stmt = $db->prepare($sql)) {
        echo $db->error;
    }
    $stmt->bind_param('i', $ID);
    if (!$stmt->execute()) {
        echo $stmt->error;
    }
    $stmt->bind_result($id, $content, $email, $name, $written);
    $comments = array();
    while ($stmt->fetch()) {
        $comments[] = array(
            'id' => $id,
            'content' => $content,
            'email' => $email,
            'name' => $name,
            'written' => $written
        );
    }
    $stmt->close();

    echo "<div align=center>";
    if (count($comments)) {
        $counter = 1;
        foreach ($comments as $comment) {
            echo "<table border='1' id='comment''>";
            echo "<tr><td><b>Kommentar #" . $counter . "</b></td></tr>";
            echo "<tr><td width='550'>" . $comment['content'] . "</td></tr>";
            $date = new DateTime($comment['written']);
            echo "<tr><td>Von <a href='mailto:" . $comment['email'] . "'>" . $comment['name'] . "</a> am " . strftime('%A, %d.%B %Y um %H:%M', $date->getTimestamp()) . " Uhr</td></tr>";
            echo "</table>";
            echo "<table><tr><td><br></td></tr></table>";
            $counter += 1;
        }
    } else {
        echo "Noch keine Kommentare vorhanden.";
    }
    echo "</div>";

    echo "<br><hr><br><b>&raquo; Kommentar verfassen</b><br><br>";
    echo "<form action='index.php?action=comment&site=add&newsid=" . $ID . "' method='post'>";
    echo "<table id='comment'><tr><td>Name*:</td><td><input name='name' type='text' size='50' maxlength='50'></td></tr>";
    echo "<tr><td>E-Mail*:</td><td><input name='email' type='text' size='50' maxlength='50'></td></tr>";
    echo "<tr><td>Kommentar*:</td><td><textarea name='comment' cols='52' rows='6'></textarea></td></tr>";
    echo "<tr><td></td><td>" . recaptcha_get_html($publickey) . "</td></tr>";
    echo "<tr><td></td><td><input type='submit' value='Kommentar absenden'></td>";
    echo "<tr><td colspan='2'>Alle mit * gekennzeichneten Felder m&uuml;ssen eingegeben werden.</td></tr>";
    echo "</table></form>";
}